<%@page import="com.mad.bikepony.model.Delivery"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<portlet:defineObjects />
<liferay-theme:defineObjects/>
<div>
<liferay-portlet:actionURL var="deliveryrequestUrl" name="addNewParcel"/>
<div>
	<p class="portlet-msg-info">
	By filling this Form you can ask to send and receive a new parcel. <br/>A confirmation email will be sent to you as soon as your request is taken into account<br/> Inside the email message you can find a <b style="color:red">tracking code</b> which can be used to track the status and location of your parcel.<br/><br/>
	When one of our agents will reach you in order to take away yor parcel , you will be asked to show the tracking code we sent you.
	<br/><br/>Remarkable suggestion : even if not mandatory , we suggest you to insert also the additional info ( like floor number , some useful indicatoion to quickly find you)
	</p>
	<div>
		<%
		Delivery delivery = (Delivery)request.getAttribute("delivery");
		if(delivery!=null)
		{
			%>
				<portlet:renderURL var="deliveryTrackUrl" >
					<portlet:param name="page" value="tracking"/>
					<portlet:param name="tracking_code" value="<%=delivery.getTrackingCode() %>"/>
				</portlet:renderURL>
				<p class="portlet-msg-success">Your parcel has been succesfully submitted.<br/>Here the tracking code you can use to know up to date state of you parcel:<b style="color:red;font-size:1.5em;" > <%=delivery.getTrackingCode() %></b></p>
				
			<%
		}%>
		<aui:form action="<%=deliveryrequestUrl.toString() %>" method="post" cssClass="deliveryRequestForm">
			<aui:fieldset label="sender">
				<br/>
				<aui:input name="sender_name" type="text" label="Name"/><!-- mandatory -->
				<span id="sender_name_error" style="display:none" class="portlet-msg-error">the name is mandatory</span>
				<aui:input name="sender_surname" type="text" label="Surname"/><!-- mandatory -->
				<span id="sender_surname_error" style="display:none"  class="portlet-msg-error">the surname is mandatory</span>
				<aui:input name="sender_email" type="email" label="Email"/><!-- mandatory -->
				<span id="sender_email_error" style="display:none" class="portlet-msg-error">please insert a valid email address</span>
				<aui:input name="sender_pickupAddress" type="text" label="Pickup address"/><!-- mandatory -->
				<span id="sender_pickupAddress_error" style="display:none" class="portlet-msg-error">thepickup address is mandatory</span>
				<aui:input name="sender_additional_info" type="text" label="Additional info"/>
			</aui:fieldset>
			<br/>
			<aui:fieldset label="recipient">
				<br/>
				<aui:input name="recipient_name" type="text" label="Name"/><!-- mandatory -->
				<span id="recipient_name_error" style="display:none" class="portlet-msg-error"> the name is mandatory</span>
				<aui:input name="recipient_surname" type="text" label="Surname"/><!-- mandatory -->
				<span id="recipient_surname_error" style="display:none" class="portlet-msg-error">the surname is mandatory</span>
				<aui:input name="recipient_email" type="email" label="Email"/><!-- mandatory -->
				<span id="recipient_email_error" style="display:none" class="portlet-msg-error">please insert a valid email address</span>
				<aui:input name="recipient_deliveryAddress" type="text" label="Delivery address"/><!-- mandatory -->
				<span id="recipient_deliveryAddress_error" style="display:none" class="portlet-msg-error">the delivery address is mandatory</span>
				<aui:input name="recipient_additional_info" type="text" label="Additional info"/>
			</aui:fieldset>
		</aui:form>
		<button onclick="validateForm()" type="button" >Send</button>

	</div>
</div>


<script type="text/javascript">

function validateForm()
{
	jQuery('span[id$="error"]').hide();
	var valid=true;
	var senderName = jQuery('input[name="<portlet:namespace/>sender_name"]').val();
	if(senderName=="")
	{
		jQuery('#sender_name_error').show();
		valid=false;		
	}
	
	var senderSurname = jQuery('input[name="<portlet:namespace/>sender_surname"]').val();
	if(senderSurname=="")
	{
		jQuery('#sender_surname_error').show();
		valid=false;		
	}
	
	
	var senderEmailAddress = jQuery('input[name="<portlet:namespace/>sender_email"]').val();
	if(senderEmailAddress=="" || !senderEmailAddress.match("([A-Za-z_][A-Za-z0-9]*)(\.[A-Za-z0-9]+)?@[a-zA-Z_][a-zA-Z0-9_]*.[a-zA-Z]{2,4}"))
	{
		jQuery('#sender_email_error').show();
		valid=false;
	}
	
	var recipientName = jQuery('input[name="<portlet:namespace/>recipient_name"]').val();
	if(recipientName=="")
	{
		jQuery('#recipient_name_error').show();
		valid=false;
	}
	
	var recipientSurname = jQuery('input[name="<portlet:namespace/>recipient_surname"]').val();
	if(recipientSurname=="")
	{
		jQuery('#recipient_surname_error').show();
		valid=false;
	}
	
	var recipientEmailAddress = jQuery('input[name="<portlet:namespace/>recipient_email"]').val();
	if(recipientEmailAddress=="" || !recipientEmailAddress.match("([A-Za-z_][A-Za-z0-9]*)(\.[A-Za-z0-9]+)?@[a-zA-Z_][a-zA-Z0-9_]*.[a-zA-Z]{2,4}"))
	{
		jQuery('#recipient_email_error').show();
		valid=false;
	}
	
	if(valid)
	{
		var pickupAddr_input = jQuery('input[name="<portlet:namespace/>sender_pickupAddress"]');
		var deliveryAddr_input = jQuery('input[name="<portlet:namespace/>recipient_deliveryAddress"]');
		var sender_pickup_addr = pickupAddr_input.val().replace(/\ /g,"+");
		var recipient_delivery_addr = deliveryAddr_input.val().replace(/\ /g,"+");
		var geocodeUrl ="http://maps.googleapis.com/maps/api/geocode/json";
		
		checkAddress(geocodeUrl,sender_pickup_addr,function(status,address){
			
			if(status==true)
			{
				var retVal = confirm("is it the right pickup address ?\n\n"+address);
				if(retVal==true)
				{
					pickupAddr_input.val(address);
				}else
				{
					valid=false;
				}
				checkAddress(geocodeUrl,recipient_delivery_addr,function(status,address2)
				{
					if(status==true)
					{
						var retVal = confirm("is it the right delivery address ?\n\n"+address2);
						if(retVal==true)
						{
							deliveryAddr_input.val(address2);
							
						}else
						{
							valid=false;
						}
						if(valid)
						{
							/*submit the total form*/
							jQuery('.deliveryRequestForm').submit();
						}
					}else
					{
						jQuery('#recipient_deliveryAddress_error').show();
					}
				});
			}else
			{
				jQuery('#sender_pickupAddress_error').show();
			}
			
		});
	}
	
}
function checkAddress(geocodeUrl,address,callback)
{
	jQuery.ajax({
		url:geocodeUrl,
		type:'GET',
		data:{
			address:address,
			sensor:false
		},
		dataType : 'json',
		success:function(data)
		{
			if(data.status=='OK')
			{
				if(data.results.length>0)
				{
					callback(true,data.results[0].formatted_address);
				}else
					callback(false);
				
			}else
			{
				callback(false);
			}
		},error:function(error , message, exception)
		{
			callback(false);
		}
	});
	
}

</script>

</div>